<template>
    <div class="login_container">
    <div class="login_box">
        <div class="avatar_box">
            <img src="../assets/light.png" alt="">
        </div>
        <el-form class="login_form" label-width="80px" :ref="login_model" :model="login_model" :rules="login_rules">
            <el-form-item label="用户名:" prop="username">
                <el-input type="primary" prefix-icon="el-icon-user" v-model="login_model.username" style="width:300px" placeholder="username"></el-input>
            </el-form-item>
            <el-form-item label="密码:" prop="password">
                <el-input type="primary" prefix-icon="el-icon-key" v-model="login_model.password" style="width:300px" placeholder="password" show-password></el-input>
            </el-form-item>
            <el-form-item>
                <el-button @click="login(login_model)">登录</el-button>
                <el-button type="info" @click="resetRef(login_model)">重置</el-button>
                <el-button @click="register">注册</el-button>
                <el-button>取消</el-button>
            </el-form-item>
        </el-form>
    </div>
    </div>
</template>
<script>
export default {
    data(){
        return{
            login_model:{
                username:'',
                passowrd:''
            },
            //表单验证
            login_rules:{
                username:[
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 3, message: '长度不少于3个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 8, message: '长度不少于8个字符', trigger: 'blur' }
                ],
            }
        }
    },
    methods:{
        //表单重置
        resetRef(formName){
            this.$refs[formName].resetFields();
        },
        login(formName){
            this.$refs[formName].validate(async valid=>{
                if(!valid){
                    this.$message({
                        type:'error',
                        message:'用户名或密码格式错误！'
                    })
                }
                else{
                    const res = await this.$http.post('login',this.login_model)
                    // console.log(res);
                    // console.log(res.data);//根据后端 成功时是OK
                    // console.log(res.status);//成功时是200
                    // console.log(typeof(res.status));//number
                    if(res.status === 200){
                        this.$message({
                            type:'success',
                            message:'登录成功！'
                        })
                        window.sessionStorage.setItem("token",res.data.token)
                        // localStorage.token = res.data.token//浏览器缓存
                        this.$router.push('/index')
                    }
                    
                }
            })//valid返回是否通过验证的布尔值

            
        },
        register(){
            this.$router.push('/register')
        }

    }
}
</script>
<style scoped>
.login_container{
    background-color: #2b4b6b;
    height: 100%;
}
.login_box{
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
}
.avatar_box{
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    background-color: #fff;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%,-50%)
}
img{
    width: 100%;
    height: 100%;
    border-radius: 50%;
}
.login_form{
    position: absolute;
    width: 100%;
    padding: 0 10px;
    box-sizing: border-box;
    bottom: 0;
}
</style>